﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using Ccit.Utils;

namespace Ccit.OA.NewsCenter
{
    public class MsClauseAttr
    {
        #region 私有属性

        private string _title;
        private string _creator;
        private string _picFlag;
        private string _topFlag;
        private string _category;
        private string _newsStatus;
        private string _dateType;
        private string _diffDays;
        private string _beginDate;
        private string _endDate;

        #endregion

        #region 公开属性

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { set { _title = value; } get { return ((!string.IsNullOrEmpty(_title)) ? _title : ""); } }

        /// <summary>
        /// 发布人
        /// </summary>
        public string Creator { set { _creator = value; } get { return ((!string.IsNullOrEmpty(_creator)) ? _creator : ""); } }

        /// <summary>
        /// 图片标志
        /// </summary>
        public string PicFlag { set { _picFlag = value; } get { return ((!string.IsNullOrEmpty(_picFlag)) ? _picFlag : ""); } }

        /// <summary>
        /// 置顶标志
        /// </summary>
        public string TopFlag { set { _topFlag = value; } get { return ((!string.IsNullOrEmpty(_topFlag)) ? _topFlag : ""); } }

        /// <summary>
        /// 栏目类别
        /// </summary>
        public string Category { set { _category = value; } get { return ((!string.IsNullOrEmpty(_category)) ? _category : ""); } }

        /// <summary>
        /// 信息状态
        /// </summary>
        public string NewsStatus { set { _newsStatus = value; } get { return ((!string.IsNullOrEmpty(_newsStatus)) ? _newsStatus : ""); } }

        /// <summary>
        /// 时间类型
        /// </summary>
        public string DateType { set { _dateType = value; } get { return ((!string.IsNullOrEmpty(_dateType)) ? _dateType : ""); } }

        /// <summary>
        /// 之前N天
        /// </summary>
        public string DiffDays 
        { 
            set 
            {
                _diffDays = (value == "") ? "0" : value; 
            } 
            get { return ((!string.IsNullOrEmpty(_diffDays)) ? _diffDays : "0"); } 
        }

        /// <summary>
        /// 开始时间
        /// </summary>
        public string BeginDate 
        { 
            set 
            {
                _beginDate = (value == "") ? "" : value; 
            } 
            get { return ((!string.IsNullOrEmpty(_beginDate)) ? _beginDate : ""); } 
        }

        /// <summary>
        /// 结束时间
        /// </summary>
        public string EndDate 
        { 
            set 
            {
                _endDate = (value == "") ? "" : value; 
            } 
            get { return ((!string.IsNullOrEmpty(_endDate)) ? _endDate : ""); } 
        }

        #endregion

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public MsClauseAttr() { }

        public MsClauseAttr(string sClauseXml)
        {
            ParseClauseXml(sClauseXml);
        }

        /// <summary>
        /// 解析查询条件XML串
        /// </summary>
        /// <param name="sClauseXml">
        /// <QUERY>
        ///     <CAPTION><![CDATA[信息标题]]></CAPTION>
        ///     <CREATOR>发布人ID</CREATOR>
        ///     <PICFLAG>导航图片</PICFLAG>
        ///     <TOPFLAG>置顶标志</TOPFLAG>
        ///     <NEWSSTATUS>信息状态</NEWSSTATUS>
        ///     <CATEGORY>栏目范围</CATEGORY>
        ///     <TIME TYPE="时间范围类型(0:全部时间范围;1:当天;2:当周;3:当月;4:之前N天;5:指定时间范围)">时间段("~"分隔)</TIME>
        /// </QUERY>
        /// </param>
        private void ParseClauseXml(string sClauseXml)
        {
            if (sClauseXml != "")
            {
                XmlDocument oDom = new XmlDocument();
                oDom.LoadXml(sClauseXml);
                if (oDom.GetElementsByTagName("QUERY").Count > 0)
                {
                    XmlNode oNodeList = oDom.GetElementsByTagName("QUERY").Item(0);
                    DateLib date = new DateLib();
                    _title = oNodeList["CAPTION"].InnerText;
                    _creator = oNodeList["CREATOR"].InnerText;
                    _picFlag = oNodeList["PICFLAG"].InnerText;
                    _topFlag = oNodeList["TOPFLAG"].InnerText;
                    _category = oNodeList["CATEGORY"].InnerText;
                    _newsStatus = oNodeList["NEWSSTATUS"].InnerText;
                    _dateType = oNodeList["TIME"].Attributes["TYPE"].InnerText;
                    if (_dateType == "0")
                    {
                        _beginDate = "";
                        _endDate = "";
                    }
                    else if (_dateType == "1") //当天
                    {
                        _beginDate = DateLib.GetSysCurDateTime(FormatType.date);
                        _endDate = DateLib.GetSysCurDateTime(FormatType.date);
                    }
                    else if (_dateType == "2")//当周
                    {
                        _beginDate = date.GetFirstDateOfWeek();
                        _endDate = date.GetLastDateOfWeek();
                    }
                    else if (_dateType == "3")//当月
                    {
                        _beginDate = date.GetFirstDateOfMonth();
                        _endDate = date.GetEndDateOfMonth();
                    }
                    else if (_dateType == "4")//之前N天内
                    {
                        _diffDays = (oNodeList["TIME"].InnerText == "") ? "0" : oNodeList["TIME"].InnerText;
                        date.Subtract(Convert.ToInt32(_diffDays));
                        _beginDate = date.ToString();
                        _endDate = DateLib.GetSysCurDateTime(FormatType.date);
                    }
                    else if (_dateType == "5")
                    {
                        string sDate = oNodeList["TIME"].InnerText;
                        if (sDate.Length > 1)
                        {
                            _beginDate = sDate.Split('~')[0].ToString();
                            _endDate = sDate.Split('~')[1].ToString();
                        }
                        else if (sDate=="~")
                        {
                            _beginDate = DateTime.Now.ToString("yyyy-MM-dd");
                            _endDate = DateTime.Now.ToString("yyyy-MM-dd");
                        }
                    }
                }
            }
        }
    }
}
